<template>
  <div class="research-output-form">
    <el-form :model="form" label-width="120px" size="small">
      <el-form-item label="成果类型" required>
        <el-select v-model="form.outputType" placeholder="请选择成果类型" @change="onOutputTypeChange">
          <el-option label="论文" value="论文" />
          <el-option label="发明专利" value="发明专利" />
          <el-option label="实用新型" value="实用新型" />
          <el-option label="软件著作权" value="软件著作权" />
        </el-select>
      </el-form-item>

      <el-form-item label="成果标题" required>
        <el-input v-model="form.title" placeholder="请输入成果标题" />
      </el-form-item>

      <el-form-item v-if="form.outputType === '论文'" label="论文类型" required>
        <el-select v-model="form.paperType" placeholder="请选择论文等级（具体类型请查阅评选制度）">
          <el-option label="CCF A类/中科院1区" value="CCF A类" />
          <el-option label="CCF B类/中科院2区等" value="CCF B类" />
          <el-option label="CCF C类/中科院3区" value="CCF C类" />
          <el-option label="EI" value="EI" />
          <el-option label="核心期刊" value="核心期刊" />
          <el-option label="普通期刊" value="普通期刊" />
        </el-select>
      </el-form-item>

      <el-form-item label="发表/授权日期" required>
        <el-date-picker
          v-model="form.publicationDate"
          type="date"
          placeholder="请选择日期"
        />
      </el-form-item>

      <el-form-item label="是否提交证明" required>
        <el-select v-model="form.hasProof" placeholder="请选择">
          <el-option label="是" :value="true" />
          <el-option label="否" :value="false" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSave">保存</el-button>
        <el-button @click="resetForm" style="margin-left: 10px;">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
import { ElMessage } from 'element-plus'

export default {
  name: 'ResearchOutputForm',
  data() {
    return {
      form: {
        outputType: '',
        title: '',
        paperType: null,
        publicationDate: null,
        hasProof: null
      }
    }
  },
  methods: {
    onOutputTypeChange(val) {
      if (val !== '论文') {
        this.form.paperType = null
      }
    },
    async handleSave() {
      const studentId = localStorage.getItem('studentId')
      if (!studentId) {
        ElMessage.error('未检测到登录学号，请重新登录')
        return
      }

      const { outputType, title, publicationDate, hasProof, paperType } = this.form
      if (!outputType || !title || !publicationDate || hasProof === null) {
        ElMessage.error('请完整填写所有必填字段')
        return
      }

      if (outputType === '论文' && !paperType) {
        ElMessage.error('请选择论文类型')
        return
      }

      // 格式化日期 yyyy-MM-dd
      let publishDateStr
      if (publicationDate instanceof Date) {
        publishDateStr = `${publicationDate.getFullYear()}-${String(publicationDate.getMonth() + 1).padStart(2, '0')}-${String(publicationDate.getDate()).padStart(2, '0')}`
      } else if (typeof publicationDate === 'string') {
        publishDateStr = publicationDate
      } else {
        ElMessage.error('日期格式不正确')
        return
      }

      console.log('发送的日期:', publishDateStr) // 调试信息

      const payload = {
        studentId: Number(studentId),
        outputType,
        title,
        contributionType: outputType === '论文' ? paperType : null,
        publicationDate: publishDateStr,
        documentProvided: hasProof
      }

      console.log('发送的数据:', payload) // 调试信息

      try {
        const res = await axios.post('/api/researchoutputs', payload, {
          headers: { 'Content-Type': 'application/json' },
          withCredentials: true
        })

        if (res.data > 0 || (res.data.code === 200)) {
          ElMessage.success('科研成果保存成功')
          this.resetForm()
        } else {
          ElMessage.error(res.data.message || '保存失败，请稍后重试')
        }
      } catch (error) {
        console.error('保存失败:', error)
        ElMessage.error('保存失败，请稍后重试')
      }
    },
    resetForm() {
      this.form = {
        outputType: '',
        title: '',
        paperType: null,
        publicationDate: null,
        hasProof: null
      }
    }
  }
}
</script>

<style scoped>
.research-output-form {
  max-width: 600px;
  margin: 0 auto;
  padding: 24px;
  background-color: #fafafa;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}
</style>
